package com.osi.mes.qm.services;

import java.util.Hashtable;
import java.util.Vector;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.ATHandler;
import com.datasweep.compatibility.client.ATRow;
import com.datasweep.compatibility.client.ATRowFilter;
import com.datasweep.compatibility.client.DatasweepException;
import com.datasweep.compatibility.client.DependentATRow;
import com.datasweep.compatibility.client.Part;
import com.datasweep.compatibility.client.PartFilter;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.ui.Time;
import com.datasweep.plantops.common.constants.filtering.IFilterComparisonOperators;
import com.datasweep.plantops.common.constants.filtering.IPartFilterAttributes;
import com.osi.mes.qm.base.constants.IATNames;
import com.osi.mes.utils.StringUtils;

public class QmBBQAssementService extends QmChkTbService implements IATNames {

	public QmBBQAssementService(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Vector<String> getBBQAssements(String assType)
			throws DatasweepException {
		Vector<String> rets = new Vector<String>();

		Vector<ATRow> bbqAssItems = getBBQAssItems(assType, null);
		if (bbqAssItems == null)
			return rets;

		for (int i = 0; i < bbqAssItems.size(); i++) {
			ATRow atRow = bbqAssItems.get(i);
			rets.add(atRow.getValue(ASSEMENT_ITEM).toString());
		}

		return rets;
	}

	public Vector<ATRow> getBBQAssItems(String assType, String assName)
			throws DatasweepException {
		String atDefName = IATNames.AT_BBQ_ASSEMENT_ITEMS;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(atDefName);
		
		if(StringUtils.isNotNull(assType))
			rowFilter.forColumnNameEqualTo(IATNames.ASSEMENT_TYPE, assType);

		if (!StringUtils.isNull(assName)) {
			rowFilter.forColumnNameEqualTo(IATNames.ASSEMENT_ITEM, assName);
		}

		Vector<ATRow> bbqAssItems = atHandler.getATRowsByFilter(rowFilter,
				false);
		return bbqAssItems;
	}

	// load all prod
	public Vector<Part> loadProds() throws DatasweepException {
		PartFilter partFilter = getFunctions().createPartFilter();
		partFilter.addSearchBy(IPartFilterAttributes.BOM_NAME,
				IFilterComparisonOperators.NOT_EQUAL_TO, null);
		Vector<Part> allParts = partFilter.exec();

		return allParts;
	}

	// 保存产品的评估项目
	public boolean saveProdAssRels(Part prod, String assType, Object[] assItems)
			throws DatasweepException {
		String atDefName = IATNames.AT_PROD_ASSEMENT_CONFIG;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		Response response = deleteProdAssItems(prod, assType);
		if (response.isError()) {
			throw new DatasweepException(response);
		}

		for (int i = 0; i < assItems.length; i++) {
			String assItem = (String) assItems[i];
			ATRow atRow = atHandler.createATRow();
			atRow.setValue(AT_PROD_ASSEMENT_ITEM_TYPE, assType);
			atRow.setValue(AT_PROD_ASSEMENT_ITEM_NAME, assItem);
			Vector<ATRow> bbqItems = getBBQAssItems(assType, assItem);
			if (bbqItems != null && bbqItems.size() > 0) {
				ATRow bbqItem = bbqItems.get(0);
				atRow.setValue(AT_PROD_ASSEMENT_ITEM_STANDARD,
						bbqItem.getValue(ASSEMENT_STANDARD));
			}

			atRow.setValue(AT_PROD_ASSEMENT_PART_NUMBER, prod.getPartNumber());
			atRow.setValue(AT_PROD_ASSEMENT_PART_REVISION,
					prod.getPartRevision());
			atRow.setValue(AT_PROD_ASSEMENT_RELATION_KEY, getSerialNum());
			response = super.saveATRow(atRow);
			if (response.isError())
				throw new DatasweepException(response);
		}

		return true;
	}

	// 删除产品的评估配置
	public Response deleteProdAssItems(Part prod, String assType)
			throws DatasweepException {
		Vector<ATRow> prodAssItems = searchProdAssRel(prod, assType, null);
		if (prodAssItems == null || prodAssItems.size() == 0)
			return new Response();

		for (int i = 0; i < prodAssItems.size(); i++) {
			ATRow atRow = prodAssItems.get(i);
			if (atRow != null) {
				Response response = super.deleteAtRow(atRow);
				if (response.isError()) {
					return response;
				}
			}
		}

		return new Response();
	}

	// 查询产品的配置项目
	public Vector<String> searchProdAssItemsAdded(Part prod, String assType)
			throws DatasweepException {
		Vector<String> rets = new Vector<String>();
		Vector<ATRow> bbqAssItems = searchProdAssRel(prod, assType, null);
		for (int i = 0; i < bbqAssItems.size(); i++) {
			ATRow atRow = bbqAssItems.get(i);
			rets.add(atRow.getValue(AT_PROD_ASSEMENT_ITEM_NAME).toString());
		}
		return rets;
	}

	// 查询产品评估的配置
	public Vector<ATRow> searchProdAssRel(Part prod, String assType, String assItem)
			throws DatasweepException {
		String atDefName = IATNames.AT_PROD_ASSEMENT_CONFIG;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(atDefName);
		if (StringUtils.isNotNull(assType))
			rowFilter.forColumnNameEqualTo(IATNames.AT_PROD_ASSEMENT_ITEM_TYPE,
					assType);

		if (prod != null) {
			rowFilter
					.forColumnNameEqualTo(
							IATNames.AT_PROD_ASSEMENT_PART_NUMBER,
							prod.getPartNumber());
			rowFilter.forColumnNameEqualTo(
					IATNames.AT_PROD_ASSEMENT_PART_REVISION,
					prod.getPartRevision());
		}

		if (!StringUtils.isNull(assItem)) {
			rowFilter.forColumnNameEqualTo(IATNames.AT_PROD_ASSEMENT_ITEM_NAME,
					assItem);
		}

		Vector<ATRow> bbqAssItems = atHandler.getATRowsByFilter(rowFilter,
				false);
		return bbqAssItems;
	}

	// 查询烧烤评估的配置，通过主键
	public ATRow searchProdAssRel(String assRelKey) throws DatasweepException {
		String atDefName = IATNames.AT_PROD_ASSEMENT_CONFIG;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(atDefName);

		if (!StringUtils.isNull(assRelKey)) {
			rowFilter.forColumnNameEqualTo(
					IATNames.AT_PROD_ASSEMENT_RELATION_KEY, assRelKey);
		}

		Vector<ATRow> atRows = atHandler.getATRowsByFilter(rowFilter, false);
		if (atRows != null && atRows.size() > 0)
			return atRows.get(0);

		return null;
	}

	// 查询烧烤评估单
	public ATRow searchBBQAssTb(String sampleRecKey) throws DatasweepException {
		String atDefName = IATNames.AT_BBQ_ASS_TB;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(atDefName);
		if (!StringUtils.isNull(sampleRecKey))
			rowFilter.forColumnNameEqualTo(AT_BBQ_ASS_TB_SAMPLE_REC_KEY,
					sampleRecKey);

		Vector<ATRow> atRows = atHandler.getATRowsByFilter(rowFilter, false);
		if (atRows != null && atRows.size() > 0)
			return atRows.get(0);

		return null;
	}

	// 查询烧烤评估的结果
	public Hashtable<String, Hashtable<ATRow, DependentATRow>> classfiedATRows(
			Vector<DependentATRow> chkResults) throws DatasweepException {
		Hashtable<String, Hashtable<ATRow, DependentATRow>> rets = new Hashtable<String, Hashtable<ATRow, DependentATRow>>();
		for (int i = 0; i < chkResults.size(); i++) {
			DependentATRow atRow = chkResults.get(i);
			String chkItemKey = (String) atRow
					.getValue(AT_BBQ_ASS_CHK_RESULT_CHK_ITEM_KEY);
			Long chkScore = (Long) atRow
					.getValue(AT_BBQ_ASS_CHK_RESULT_CHK_VAL);
			ATRow assConfig = searchProdAssRel(chkItemKey);
			if (assConfig == null)
				continue;

			String assType = assConfig.getValue(AT_PROD_ASSEMENT_ITEM_TYPE).toString();
			if (!rets.containsKey(assType)) {
				Hashtable<ATRow, DependentATRow> rowData = new Hashtable<ATRow, DependentATRow>();
				rets.put(assType, rowData);
			}

			rets.get(assType).put(assConfig, atRow);
		}

		return rets;
	}

	// 查询烧烤评估结果
	public Vector<ATRow> searchAssTbs(Time createStartTime,Time createEndTime, String orderNumber,
			String partNumber) throws DatasweepException {
		String atDefName = IATNames.AT_BBQ_ASS_TB;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(atDefName);
		if (createStartTime != null) {
			rowFilter.forCreatedTimeGreaterThanOrEqualTo(getDayStart(createStartTime));
		}
		
		if(createEndTime!=null)
		{
			rowFilter.forCreatedTimeLessThan(getDayEnd(createEndTime));
		}

		if (!StringUtils.isNull(partNumber)) {
			rowFilter.forColumnNameEqualTo(AT_BBQ_ASS_TB_PART_NUMBER,
					partNumber);
		}

		Vector<ATRow> atRows = atHandler.getATRowsByFilter(rowFilter, false);
		if (atRows == null)
			return new Vector<ATRow>();

		return atRows;
	}
	
}
